package com.aop.SpElAop.ExpressionParser;

/**
 * @Package: com.aop.SpElAop
 * @ClassName: LogEvaluationContext
 * @Author: wulongkai
 * @CreateTime: 2024/7/9 10:58
 * @Description:
 */

import com.aop.SpElAop.LogAop.Logging;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.ParameterNameDiscoverer;

import java.lang.reflect.Method;

/**
 * SpEl 解析时的上下文信息
 * 凡是在${@link CommonEvaluationContext#setVariable(String, Object)}中加入的变量，都可在${@link Logging} 注解的SpEL表达式中使用
 */
public class CommonEvaluationContext extends MethodBasedEvaluationContext {

    /**
     * 保存结果对象的变量的名称。
     */
    public static final String RESULT_VARIABLE = "result";
    public static final String ERROR_MSG_VARIABLE = "errorMsg";

    /**
     * 把方法的参数、返回值和 ErrorMsg 都放到 RootObject 中
     */
    public CommonEvaluationContext(Object rootObject, Method method, Object[] args,
                                   ParameterNameDiscoverer parameterNameDiscoverer,
                                   Object result, String errorMsg) {
        super(rootObject, method, args, parameterNameDiscoverer);
        setVariable(RESULT_VARIABLE, result);
        setVariable(ERROR_MSG_VARIABLE, errorMsg);
    }
}


